<?php
/*
	本插件：$this->model('kabao')->方法名();
	其他地方调用：model('jy_weishop/kabao')->方法名();
*/
namespace jy_weishop;
defined('BY_JYA') or exit('error');
class kabao extends \plugin{
	function add_payorder($goods=array(),$mid=0,$times=0,$buyprice=0){
		$goods['goodsids']=json_decode($goods['goodsids'],true);
		$credit_types=credit_types();
		foreach($goods['goodsids'] as &$sv){
			$sv['goods']=pdo_get('jy_weishop_goods',array('id'=>$sv['id']),array('title','image','price','subtitle','type'));
		}
		$ordersn='SN'.get_ordersn('jy_weishop_goods_packageorder','ordersn');
		$data=array(
			'listid'=>$goods['id'],
			'ordersn'=>$ordersn,
			'jiangs'=>ijson_encode($goods['goodsids']),
			'total'=>$times?:$goods['times'],
			'lastnum'=>$times?:$goods['times'],
			'payfee'=>$buyprice?:$goods['buyprice'],
			'status'=>6,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$mid?:$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_weishop_goods_packageorder',$data);
		$orderid=pdo_insertid();
		return $orderid;
	}
	function get_jiangs($orderid=0){
		$order=pdo_get('jy_weishop_goods_packageorder',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		if($order['status'] || $order['lastnum']<=0){
			exi('该礼包已送完或礼包已兑换！','error');
		}
		$jiangs=json_decode($order['jiangs'],true);
		$status=5;
		foreach($jiangs as $sv){
			if(!$sv['type']){
				$sv['goods']=pdo_get('jy_weishop_goods',array('id'=>$sv['goodsid']),array('title','image','price','subtitle','type'));
				$status=1;
			}elseif($sv['type']==1){
				model('jy_weishop/coupon')->getcoupon($sv['couponid'],$_SESSION['uid'],1);
			}elseif($sv['type']==2){
				$red=array(
					'mid'=>$_SESSION['uid'],
					'money'=>$sv['num'],
					'plugin'=>'jy_kabao',
					'remark'=>'卡包兑奖现金红包',
				);
				cfc('red_pack')->get_money($red);
				$sv['status']=5;
			}elseif($sv['type']==3){
				if($sv['credit_type'] && $sv['num']){
					cfc('core')->member_credit($_SESSION['uid'],'credit'.$sv['credit_type'],$sv['num'],1,'jy_kabao','卡包兑奖');
				}
			}
		}
		if($order['lastnum']==1){
			pdo_update('jy_weishop_goods_packageorder',array('status'=>$status),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		}else{
			pdo_update('jy_weishop_goods_packageorder',array('lastnum -='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
			$ordersn='SN'.get_ordersn('jy_weishop_goods_packageorder','ordersn');
			$data=array(
				'listid'=>$order['listid'],
				'diyinfo'=>$order['diyinfo'],
				'ordersn'=>$ordersn,
				'jiangs'=>$order['jiangs'],
				'total'=>1,
				'lastnum'=>1,
				'frommid'=>$order['mid'],
				'fromorderid'=>$order['id'],
				'status'=>$status,
			);
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$order['mid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_weishop_goods_packageorder',$data);
		}
		return $orderid;
	}
	public function refund_pay($id=0){
		$item=pdo_get('jy_weishop_goods_packageorder',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		if($item['status']){
			exi('该订单不支持退款！','error');
		}
		$item['lsprice']=$item['lastnum']<$item['total']?$item['lastnum']*$item['payfee']/$item['total']:$item['payfee'];
		$paylog=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pay_id']));
		if(!$paylog['id']){
			exi('该订单的支付记录不存在！','error');
		}
		$transaction_id=$paylog['transaction_id'];
		if($paylog['pay_type']=='credit' || $paylog['pay_type']=='apipay'){
			$pay_out_id=model('core/member')->add_credit2($paylog['mid'],$item['lsprice'],'订单退款-'.$item['id'],'jy_kabao');
		}elseif(strpos($paylog['pay_type'],'credit') !== false){
			$pay_out_id=model('core/member')->member_credit($paylog['mid'],$paylog['pay_type'],$item['lsprice'],true,'','订单退款');
		}elseif($paylog['pay_type']=='wechat'){
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有微信支付号！','error');
			}
			$paylog['transid']=$transaction_id;
			$paylog['money']=$item['lsprice'];
			$paylog['remark']='订单退款';
			$pay_out_id=model('core/member')->refund_out($paylog['mid'],$paylog,$noexi);
		}elseif($paylog['paytype']=='alipay'){
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有支付宝支付号！','error');
			}
			$params=array(
				'money'=>$item['lsprice'],
				'ordersn'=>$item['ordersn'],
				'transid'=>$transaction_id,
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->refund_out($paylog['mid'],$params,$noexi,'alipay');
		}else{
			$params=array(
				'money'=>$item['lsprice'],
				'ordersn'=>$item['ordersn'],
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->pay_out($paylog['mid'],$params,$noexi);
		}
		$update=array(
			'status'=>9,
			'tuifee'=>$item['lsprice'],
			'tuitime'=>TIMESTAMP,
			'pay_out_id'=>$pay_out_id,
		);
		pdo_update('jy_weishop_goods_packageorder',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}
}
?>